<?php
class Formitems extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 20;
        $this->_form_type = 'form_items';
        $this->_list_where = array();
        $this->_action_list = array('EDIT');
        $this->_controller = 'admin/formitems/';
        parent::__construct();
    }

    public function showList($id = 0)
    {
        $this->_external_id = $id;
        $res = $this->adminform_model->getTableById($id);
        $this->_list_where = array('name'=>$res['name']);

        $res = $this->getList();
        $createbtn = $this->load->view('admin/form/createbtn',
            array('controller'=>site_url($this->_controller),'param'=>$this->_external_id),true);
        $backbtn = $this->load->view('admin/form/backbtn',array('controller'=>site_url('admin/form/')),true);
        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'createbtn'=>$createbtn,'backbtn'=>$backbtn);

        $content = $this->load->view('admin/form/list',$data,true);
        echo $this->getAdminView($content);

    }

    public function update($id = 0,$formid = 0)
    {
        $this->_external_id = $formid;
        $res = $this->adminform_model->getTableById($formid);
        $post = $this->input->post();

        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),
                'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            //filter post data
            $post['name']  = $res['name'];
            $post['tablename']  = $res['tablename'];
            $post['sort'] = (int)trim($post['sort']);
            $post['cname'] = trim($post['cname']);
            $post['type']  = trim($post['type']);
            $post['ckey']  = trim($post['ckey']);
            $post['class'] = trim($post['class']);
            $post['value'] = empty($post['value'])?'':trim($post['value']);
            $post['func']  = empty($post['func'])?'':trim($post['func']);
            $post['param']  = empty($post['param'])?'':trim($post['param']);
            $post['request']  = (int)trim($post['request']);

            $this->_update_data = $post;
            parent::update($id);
        }
    }

    public function create($id = 0)
    {
        $this->_external_id = $id;
        $res = $this->adminform_model->getTableById($id);
        $post = $this->input->post();

        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),
                'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            //filter post data
            $post['name']  = $res['name'];
            $post['tablename']  = $res['tablename'];
            $post['sort'] = (int)trim($post['sort']);
            $post['cname'] = trim($post['cname']);
            $post['type']  = trim($post['type']);
            $post['ckey']  = trim($post['ckey']);
            $post['class'] = trim($post['class']);
            $post['value'] = empty($post['value'])?'':trim($post['value']);
            $post['func']  = empty($post['func'])?'':trim($post['func']);
            $post['param']  = empty($post['param'])?'':trim($post['param']);
            $post['request']  = (int)trim($post['request']);

            $this->_create_data = $post;
            parent::create();
        }
    }

    public function delete($id){return false;}
}